﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class sysadmin_Login : System.Web.UI.Page
{
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    PowerCreator.LMS.Web.DL.User.WebAdvancedUser opAdvancedUser = new PowerCreator.LMS.Web.DL.User.WebAdvancedUser();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            this.imgbtn_Login.Attributes.Add("OnClick", "return chkForm()");
            
        }
    }
    private void InitPage()
    {
        //lbl_UserName.Text = config.GetLanagerValueByKey("Login_lbl_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_UserPwd.Text = config.GetLanagerValueByKey("Login_lbl_UserPwd", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Code.Text = config.GetLanagerValueByKey("Login_lbl_Code", PowerCreator.LMS.Language.LanagetItemType.Page);
    }
    private bool CheckPage()
    {
        if (string.IsNullOrEmpty(txtCode.Text))
        {
            //lblLoginMsg.Text = config.GetLanagerValueByKey("Login_msg_code1", PowerCreator.LMS.Language.LanagetItemType.Page);
            //Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            PowerCreator.LMS.Common.Script.Script.Alert(this, "msg", config.GetLanagerValueByKey("Login_msg_code1", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        if (Session["CheckCode"] != null)
        {
            string s = Session["CheckCode"].ToString();
            if (s.ToLower() != txtCode.Text.ToLower())
            {
                //lblLoginMsg.Text = config.GetLanagerValueByKey("Login_msg_code2", PowerCreator.LMS.Language.LanagetItemType.Page);
                PowerCreator.LMS.Common.Script.Script.Alert(this, "msg", config.GetLanagerValueByKey("Login_msg_code2", PowerCreator.LMS.Language.LanagetItemType.Page));
                txtCode.Text = "";
                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }

        
        
        
        if (string.IsNullOrEmpty(txtUserName.Text)) return false;
        if (string.IsNullOrEmpty(txtPassword.Text)) return false;
        txtCode.Text = "";
        return true;
    }
    protected void imgbtn_Login_Click(object sender, ImageClickEventArgs e)
    {
        //登录信息
        PowerCreator.LMS.Web.DL.User.LoginInfo info = new PowerCreator.LMS.Web.DL.User.LoginInfo();

        PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();

        //检查加密狗已放置通用函数
        if (!CheckPage()) return;

        info.LoginIp = PowerCreator.LMS.Common.Utils.IPUtils.GetIP;
        info.LoginTime = DateTime.Now;
        info.UserName = txtUserName.Text;
        info.UserPwd = txtPassword.Text;

        string UserLocation = GetUserLocation();

        if (opAdvancedUser.Login(info, ref login, ConfigurationManager.AppSettings["CheckLoginIP"].ToString(), UserLocation))
        {
            Session.Add(PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO, login);
            //login.VOOnlineUser = opUser.AddOnlineUser(info, login.VOUser.UserId);
            //opUser.UpdateOnlineUserList();
            string url = "MainFrame.htm";
            Response.Redirect(url);
        }
        else
        {
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtCode.Text = "";
            txtUserName.Focus();
            string msg = config.GetLanagerValueByKey(login.LoginMessage, PowerCreator.LMS.Language.LanagetItemType.MSG);
            //lblLoginMsg.Text = msg;
            //opUser.AddOnlineUser(info, PowerCreator.LMS.Web.User.User.LoginNoUserID);
            //Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            PowerCreator.LMS.Common.Script.Script.Alert(this, "al", msg);
        }
    }
    protected void imgbtn_Reset_Click(object sender, ImageClickEventArgs e)
    {
        txtUserName.Text = "";
        txtPassword.Text = "";
        txtCode.Text = "";
        txtUserName.Focus();
    }
    public string GetUserLocation()
    {
        try
        {
            WebClient webGetting = new WebClient();
            string userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            string ipQueryResult = webGetting.DownloadString("http://www.ip.cn/getip.php?action=queryip&ip_url=" + userIP);
            string startString = @"来自：";
            int startIndex = ipQueryResult.LastIndexOf(startString) + startString.Length;
            int endIndex = ipQueryResult.LastIndexOf(@" ", startIndex);
            return ipQueryResult.Substring(startIndex, ipQueryResult.Length - startIndex);
        }
        catch
        {
            return string.Empty;
        }
    }

}
